<!DOCTYPE HTML>
<html lang="en">
<head>
<title>StringMid - Syntax &amp; Usage | AutoHotkey</title>
<meta name="description" content="The StringMid command retrieves one or more characters from the specified position in a string." />
<meta name="ahk:equiv-v2" content="commands/SubStr.htm" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>StringMid</h1>

<p>Retrieves one or more characters from the specified position in a string.</p>

<p class="warning"><strong>Deprecated:</strong> This command is not recommended for use in new scripts. Use the <a href="SubStr.htm">SubStr</a> function instead.</p>

<pre class="Syntax"><span class="func">StringMid</span>, OutputVar, InputVar, StartChar <span class="optional">, Count, L</span></pre>
<h2>Parameters</h2>
<dl>

  <dt>OutputVar</dt>
  <dd><p>The name of the variable in which to store the substring extracted from <em>InputVar</em>.</p></dd>

  <dt>InputVar</dt>
  <dd><p>The name of the variable from whose contents the substring will be extracted.  Do not enclose the name in percent signs unless you want the <em>contents</em> of the variable to be used as the name.</p></dd>

  <dt>StartChar</dt>
  <dd><p>The position of the first character to be extracted, which can be an <a href="../Variables.htm#Expressions">expression</a>. Unlike <a href="StringGetPos.htm">StringGetPos</a>, 1 is the first character. If <em>StartChar</em> is less than 1, it will be assumed to be 1. If <em>StartChar</em> is beyond the end of the string, <em>OutputVar</em> is made empty (blank).</p></dd>

  <dt>Count</dt>
  <dd><p><span class="ver">[v1.0.43.10+]</span>: This parameter may be omitted or left blank, which is the same as specifying an integer large enough to retrieve all characters from the string.</p>
      <p>Otherwise, specify the number of characters to extract, which can be an <a href="../Variables.htm#Expressions">expression</a>. If <em>Count</em> is less than or equal to zero, <em>OutputVar</em> will be made empty (blank). If <em>Count</em> exceeds the length of <em>InputVar</em> measured from <em>StartChar</em>, <em>OutputVar</em> will be set equal to the entirety of <em>InputVar</em> starting at <em>StartChar</em>.</p></dd>

  <dt>L</dt>
  <dd><p>The letter L can be used to extract characters that lie to the left of <em>StartChar</em> rather than to the right. In the following example, <em>OutputVar</em> will be set to <em>Red</em>:</p>
<pre>InputVar := "The Red Fox"
StringMid, OutputVar, InputVar, 7, 3, L</pre>
    <p>If the L option is present and <em>StartChar</em> is less than 1, <em>OutputVar</em> will be made blank. If <em>StartChar</em> is beyond the length of <em>InputVar</em>, only those characters within reach of <em>Count</em> will be extracted. For example, the below will set <em>OutputVar</em> to <em>Fox</em>:</p>
<pre>InputVar := "The Red Fox"
StringMid, OutputVar, InputVar, 14, 6, L</pre>
</dd>

</dl>

<h2>Remarks</h2>
<p>For this and all other commands, <em>OutputVar</em> is allowed to be the same variable as <em>InputVar</em>.</p>
<h2>Related</h2>
<p><a href="SubStr.htm">SubStr()</a>, <a href="StringLeft.htm">StringLeft</a>, <a href="StringLeft.htm">StringRight</a>, <a href="StringTrimLeft.htm">StringTrimLeft</a>, <a href="StringTrimLeft.htm">StringTrimRight</a>, <a href="IfInString.htm">IfInString</a>, <a href="StringGetPos.htm">StringGetPos</a>, <a href="StringLen.htm">StrLen() / StringLen</a>, <a href="StringLower.htm">StringLower</a>, <a href="StringLower.htm">StringUpper</a>, <a href="StringReplace.htm">StringReplace</a></p>
<h2>Examples</h2>
<div class="ex" id="ExBasic">
<p><a href="#ExBasic">#1</a></p>
<pre>Source := "Hello this is a test."
StringMid, the_word_this, Source, 7, 4</pre>
</div>

</body>
</html>
